//
//  ViewController.m
//  collectionViewDemo
//
//  Created by xingzhi on 16/8/4.
//  Copyright © 2016年 北京开科唯识技术有限公司. All rights reserved.
//

#import "ViewController.h"
#import "cutomCell.h"
#define kCellID @"cellID"
#define kReusableHeaderView @"reusableHeaderView"
#define kReusableFooterView @"reusableFooterView"


@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController{
    UICollectionView *mainCollectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //初始化 layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    //初始化 collectionView
    mainCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    mainCollectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mainCollectionView];
    
    
    //注册事件
    [mainCollectionView  registerClass:[cutomCell class] forCellWithReuseIdentifier:kCellID];
    [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kReusableHeaderView];
    [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kReusableFooterView];

    
    
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;

    
}

-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    cutomCell *cell = (cutomCell*)[collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
   //防重影
    for (UIView *view in cell.subviews) {
        [view removeFromSuperview];
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 50, 30)];
    label.text = [NSString stringWithFormat:@"%ld 行,%ld 列",indexPath.section,indexPath.row];
    label.adjustsFontSizeToFitWidth = YES;
    [cell.contentView addSubview:label];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(100, 200);
}
//footer的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 100);
}

//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 50);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *view;
    NSString *contentStr;
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        view  = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kReusableFooterView forIndexPath:indexPath];
        view.backgroundColor =[UIColor greenColor];
        contentStr = [NSString stringWithFormat:@"第 %ld 个FooterView",indexPath.section];
      
    }else{
       view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kReusableHeaderView forIndexPath:indexPath];
        view.backgroundColor =[UIColor grayColor];
        contentStr = [NSString stringWithFormat:@"第 %ld 个headerView",indexPath.section];

    }
    //防重影
    for (UIView *obj in view.subviews) {
        [obj removeFromSuperview];
    }
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
    label.text = contentStr;
    label.font = [UIFont systemFontOfSize:20];
    [view addSubview:label];
    return view;

  
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld,  %ld",indexPath.section,indexPath.row);
}






@end
